package com.justcan.demo.cartoon.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import com.justcan.demo.cartoon.R;
import com.justcan.demo.cartoon.utils.anim.AnimObject;
import com.justcan.demo.cartoon.utils.interpolator.EaseOutBounceInterpolator;


/**
 * 发射台
 */
public class LaunchPadObject extends AnimObject {

	/**
	 * 宽高
	 */
	private float mWidth = 0;
	private float mHeight = 0;
	/**
	 * 坐标
	 */
	private int mX = 0;
	private int mY = 0;
	/**
	 * 是否已经初始化
	 */
	private boolean mHasInit = false;
	/**
	 * 图
	 */
	private Bitmap mBitmap = null;
	/**
	 * 绘制Rect
	 */
	private Rect mSrcRect = null;
	private Rect mDstRect = null;
	/**
	 * Paint
	 */
	private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
	/**
	 * ColorMatrixColorFilter
	 */
	private ColorMatrixColorFilter mColorMatrixColorFilter = null;
	/**
	 * 火箭的Object
	 */
	private RocketObject mRocketObject = null;
	/**
	 * 上升的平移动画
	 */
	private LaunchPadTransAnimation mUpTransAnimation = new LaunchPadTransAnimation();
	/**
	 * 下降的平移动画
	 */
	private LaunchPadTransAnimation mDownTransAnimation = new LaunchPadTransAnimation();
	/**
	 * 是否要高亮
	 */
	private boolean mIsHighLight = false;
	/**
	 * 火花的图片
	 */
	private Bitmap[] mFireBmps = null;
	/**
	 * 当前火花图片
	 */
	private int mFireIndex = 0;
	/**
	 * 火花的速度,100ms切换下一张图片
	 */
	private int mFireSpeed = 100;
	/**
	 * 上一次火花动画时间
	 */
	private long mLastLogicFireTime = 0l;
	/**
	 * 是否绘制火花
	 */
	private boolean mIsDrawFire = false;
	/**
	 * 火花宽高
	 */
	private float mFireWidth = 0;
	private float mFireHeight = 0;
	/**
	 * 火花绘制Rect
	 */
	private Rect mFireSrcRect = null;
	private Rect mFireDstRect = null;
	/**
	 * 发射台上方阴影的高度
	 */
	private float mLaunchPadShadowHeight = 0;

	public LaunchPadObject(View mRootAnimView, Context mContext) {
		super(mRootAnimView, mContext);
		if (getAnimSceneWidth() != 0 && getAnimSceneHeight() != 0) {
			init();
		}
	}

	@Override
	public void onSizeChange(int w, int h) {
		super.onSizeChange(w, h);
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		if (!mHasInit) {
			mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.launch_pad);
			int mSceneWidth = getAnimSceneWidth();
			int mSceneHeight = getAnimSceneHeight();
			mWidth = mSceneWidth;	// 宽度
			mHeight = mWidth * 0.342f;		// 根据图片计算合理的高度
			mX = 0;			// 定义初始位置
			mY = (int) (mSceneHeight - mHeight * 0.845f);
			mSrcRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
			mDstRect = new Rect(mX, mY, (int) (mWidth + mX), (int) (mHeight + mY));
			// 使图片变暗的ColorMatrix
			ColorMatrix colorMatrix = new ColorMatrix(new float[]{
					0.5F, 0, 0, 0, 0,
					0, 0.5F, 0, 0, 0,
					0, 0, 0.5F, 0, 0,
					0, 0, 0, 1, 0,
			});
			mColorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
			// 动画初始化
			mUpTransAnimation.cancel();
			mDownTransAnimation.cancel();
			mLaunchPadShadowHeight = mHeight * 0.70f;
			// 火花
			mFireBmps = new Bitmap[] {
					BitmapFactory.decodeResource(getContext().getResources(), R.drawable.fire_1),
					BitmapFactory.decodeResource(getContext().getResources(), R.drawable.fire_2),
					BitmapFactory.decodeResource(getContext().getResources(), R.drawable.fire_3),
			};
			mFireWidth = mWidth / 2;	// 大概是发射台宽的1/2
			mFireHeight = mFireWidth * 0.53f;
			mFireSrcRect = new Rect(0, 0, mFireBmps[0].getWidth(), mFireBmps[0].getHeight());
			mFireDstRect = new Rect((int) ((mWidth - mFireWidth) / 2), (int) (mY - mFireHeight + mLaunchPadShadowHeight),
					(int) ((int) ((mWidth - mFireWidth) / 2) + mFireWidth), (int) (mY + mLaunchPadShadowHeight));
			mHasInit = true;
		}
	}

	@Override
	public void logic(long animTime, long deltaTime) {
		if (mRocketObject != null) {
			// 处理发射台的逻辑
			int rocketBottom = mRocketObject.getRocketRect().bottom;
			if (rocketBottom > getAnimSceneHeight() * 2 / 3) {
				mIsHighLight = true;
				// 如果火箭的位置超过屏幕的 2/3，发射台变亮并上升
				if (!mUpTransAnimation.hasStarted() || mUpTransAnimation.hasEnded()) {
					mDownTransAnimation.cancel(); 		// 取消下降的动画
					// 创建新的动画
					mUpTransAnimation.mStartX = mX;
					mUpTransAnimation.mStartY = mY;
					mUpTransAnimation.mEndX = mX;
					mUpTransAnimation.mEndY = (int) (getAnimSceneHeight() - mHeight);
					mUpTransAnimation.setStartTime(Animation.START_ON_FIRST_FRAME);
					mUpTransAnimation.setDuration(600);
					mUpTransAnimation.setInterpolator(new EaseOutBounceInterpolator());
					// 开始动画
					mUpTransAnimation.getTransformation(animTime, null);
				} else {
					// 已经在动画中
					mUpTransAnimation.getTransformation(animTime, null);
				}
			} else {
				mIsHighLight = false;
				// 如果火箭的位置没有超过屏幕的 2/3，发射台恢复一开始的状态
				int initStartY = (int) (getAnimSceneHeight() - mHeight * 0.845f);
				if (mY < initStartY) {
					if (!mDownTransAnimation.hasStarted() || mDownTransAnimation.hasEnded()) {
						mUpTransAnimation.cancel();		// 取消上升的动画
						// 创建新的动画
						mDownTransAnimation.mStartX = mX;
						mDownTransAnimation.mStartY = mY;
						mDownTransAnimation.mEndX = mX;
						mDownTransAnimation.mEndY = initStartY;
						mDownTransAnimation.setStartTime(Animation.START_ON_FIRST_FRAME);
						mDownTransAnimation.setDuration(600);
						// 开始动画
						mDownTransAnimation.getTransformation(animTime, null);
					} else {
						// 已经在动画中
						mDownTransAnimation.getTransformation(animTime, null);
					}
				}
			}
			mDstRect.set(mX, mY, (int) (mWidth + mX), (int) (mHeight + mY));
			// 处理火花的逻辑
			if (mY - 10 <= getAnimSceneHeight() - mHeight) {
				// 出现火花
				mIsDrawFire = true;
				if (isReadyOnLaunch()) {
					mFireSpeed = 20;
				} else {
					mFireSpeed = 100;
				}
				if (mLastLogicFireTime == 0l) {
					mLastLogicFireTime = animTime;
				}
				long now = System.currentTimeMillis();
				if (now - mLastLogicFireTime > mFireSpeed) {
					mFireIndex++;
					mFireIndex = mFireIndex % mFireBmps.length;
					mLastLogicFireTime = now;
				}
				mFireDstRect.set((int) ((mWidth - mFireWidth) / 2), (int) (mY - mFireHeight + mLaunchPadShadowHeight),
						(int) ((int) ((mWidth - mFireWidth) / 2) + mFireWidth), (int) (mY + mLaunchPadShadowHeight));
			} else {
				// 火花消失
				mIsDrawFire = false;
			}
		}
	}

	@Override
	public void draw(Canvas canvas, int sceneWidth, int sceneHeight) {
		if (mHasInit) {
			if (mIsHighLight) {
				mPaint.setColorFilter(null);
			} else {
				mPaint.setColorFilter(mColorMatrixColorFilter);
			}
			canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, mPaint);
			if (mIsDrawFire) {
				canvas.drawBitmap(mFireBmps[mFireIndex], mFireSrcRect, mFireDstRect, mPaint);
			}
		}
	}

	public void setRocketObject(RocketObject object) {
		mRocketObject = object;
	}

	/**
	 * 是否处于准备发射阶段，也就是火箭位于发射口的位置
	 */
	public boolean isReadyOnLaunch() {
		if (mRocketObject != null) {
			int rocketLeft = mRocketObject.getRocketRect().left;
			int rocketRight = mRocketObject.getRocketRect().right;
			int rocketBottom = mRocketObject.getRocketRect().bottom;
			if (rocketBottom > getAnimSceneHeight() * 5 / 6
					&& rocketLeft > mFireDstRect.left
					&& rocketRight < mFireDstRect.right) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 发射台的平移动画
	 */
	class LaunchPadTransAnimation extends Animation {

		/**
		 * 起始位置
		 */
		public int mStartX = 0;
		public int mStartY = 0;
		/**
		 * 预期结束位置
		 */
		public int mEndX = 0;
		public int mEndY = 0;

		@Override
		protected void applyTransformation(float interpolatedTime,
										   Transformation t) {
			// 改变真实的位置坐标
			mX = (int) (mStartX + (mEndX - mStartX) * interpolatedTime);
			mY = (int) (mStartY + (mEndY - mStartY) * interpolatedTime);
		}
	}
}
